/*
 * Copyright (C) 2010 ZXing authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.yyfax.ph.common.scan.idcard;

import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.yyfax.ph.common.scan.Utils.ViewUtil;

import exocr.exocrengine.EXIDCardResult;
import exocr.exocrengine.EXOCREngine;

class DecodeHandler extends Handler {
	private final CaptureListener captureListener;

	private int decode_id;
	private int decode_succeeded_id;
	private int decode_failed_id;
	private int quit_id;
	
  DecodeHandler(CaptureListener captureListener) {
    this.captureListener = captureListener;
	decode_id = ViewUtil.getResourseIdByName( this.captureListener.getAPPpackageName(), "id", "decode");
	decode_succeeded_id = ViewUtil.getResourseIdByName(this.captureListener.getAPPpackageName(), "id", "decode_succeeded");
	decode_failed_id = ViewUtil.getResourseIdByName(this.captureListener.getAPPpackageName(), "id", "decode_failed");
	quit_id = ViewUtil.getResourseIdByName(this.captureListener.getAPPpackageName(), "id", "quit");
  }
  
  
  @Override
  public void handleMessage(Message message) {
		if (message.what == decode_id) {
			decode((byte[]) message.obj, message.arg1, message.arg2);
		} else if (message.what == quit_id) {
			try {
				Looper.myLooper().quit();
			} catch (NullPointerException e) {
				e.printStackTrace();
			}
		}
  }

  /**
   * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
   * reuse the same reader objects from one decode to the next.
   *
   * @param data   The YUV preview frame.
   * @param width  The width of the preview frame.
   * @param height The height of the preview frame.
   */
	private void decode(byte[] data, int width, int height) {
		int[] rects = new int[32];
		int ret;
		EXOCREngine ocrengine = new EXOCREngine();

		ocrengine.timestart = System.currentTimeMillis();
		ret = EXOCREngine.nativeRecoIDCardRawdat(data, width, height, width, 1, ocrengine.bResultBuf, ocrengine.bResultBuf.length);
		ocrengine.timeend   = System.currentTimeMillis();
		
		if (ret > 0) {
			EXIDCardResult idcard = EXIDCardResult.decode(ocrengine.bResultBuf, ret);
			if ( idcard != null  ) {
				idcard.SetViewType("Preview");
				idcard.SetColorType(CardColorJudge(data, width, height));
				
				//NOTE: 下面这些代码是提取标准身份证图像的，如果客户有需求请打开
				Bitmap imcard = EXOCREngine.nativeGetIDCardStdImg(data, width, height, ocrengine.bResultBuf, ocrengine.bResultBuf.length, rects);
				if (imcard != null) {
					idcard.SetBitmap(imcard);
					// 保存各个条目的矩形框
					idcard.setRects(rects);
					// 返回图像结束-------END

					Message message = Message.obtain(captureListener.getHandler(), decode_succeeded_id, idcard);
					message.sendToTarget();
					return;
				}
			}
		}
		Message message = Message.obtain(captureListener.getHandler(), decode_failed_id);
		message.sendToTarget();
	}
	
	private int CardColorJudge(byte[] data, int width, int height) {
		int offset = width * height;
		int i, j;
		int top, btm, lft, rgt;
		int iTht = 144;
		int iCnt = 255;
		int nNum = 0;

		top = height / 16;
		btm = height / 2 - height / 16;
		lft = width / 16;
		lft *= 2;
		rgt = width / 2 - width / 16;
		rgt *= 2;
		for (i = top; i < btm; ++i) {
			for (j = lft; j < rgt; ++j) {
				int val = data[i * width + j + offset] & 0xFF;
				if (val > iTht) {
					++nNum;
				}
			}
		}
		if (nNum > iCnt)
			return 1;
		else
			return 0;
	}
	

}
